package eclipse.lib.graphics.primitives;
import com.sun.opengl.util.texture.*;
import javax.media.opengl.*;
import eclipse.lib.*;
import eclipse.lib.graphics.shaders.GLColor;
/**
 * GLPolygon represents the most basic 3D object, a polygon.
 */
public class GLPolygon 
{
	public int			renderState;
	private int 		pointCount;
	private Position 	point[];
	private float		u[];
	private float 		v[];
	private GLColor	 	color[];
	private Texture 	texture;
	/**
	 * Creates a polygon with specified number of points
	 * @param points Number of points the polygon has
	 */
	public GLPolygon(int points)
	{
		pointCount = points;
		point 	= new Position[points];
		u		= new float[points];
		v		= new float[points];
		color	= new GLColor[points];
		for(int i = 0; i < points; i++)		
			color[i] = new GLColor(1f, 1f, 1f);		
	}
	/**
	 * Returns the number of points the polygon has
	 * @return The number of points the polygon has
	 */
	public int getPointCount()
	{
		return this.pointCount;
	}
	/**
	 * Returns a point of the polygon
	 * @param pIndex Index of the point to return
	 * @return A point of the polygon with the index of pIndex
	 */
	public Position getPoint(int pIndex)
	{
		return this.point[pIndex];
	}
	/**
	 * Sets the UV mapping for the polygon
	 * @param pIndex Point to UV map
	 */
	public void setUV(int pIndex, float u, float v)
	{
		this.u[pIndex] = u;
		this.v[pIndex] = v;
	}
	public float getU(int pIndex)
	{
		return this.u[pIndex];
	}
	public float getV(int pIndex)
	{
		return this.v[pIndex];
	}
	public void setPoint(int pIndex, float x, float y, float z, float u, float v)
	{
		setUV(pIndex, u, v);
		setPoint(pIndex, x, y, z, u, v);
	}
	public void setPoint(int pIndex, Position pos, float u, float v)
	{
		setUV(pIndex, u, v);
		setPoint(pIndex, pos);
	}
	public void setPoint(int pIndex, float x, float y, float z)
	{
		this.point[pIndex] = new Position(x, y, z);
	}
	public void setPoint(int pIndex, Position pos)
	{
		this.point[pIndex] = pos;
	}
	public void setColor(int pIndex, GLColor color)
	{
		this.color[pIndex] = color;
	}
	public void setColor(int pIndex, float a, float r, float g, float b)
	{
		this.color[pIndex] = new GLColor(a, r, g, b);
	}
	public GLColor getColor(int pIndex)
	{
		return this.color[pIndex];
	}
	public void setTexture(Texture texture)
	{
		this.texture = texture;
	}
	public Texture getTexture()
	{
		return this.texture;
	}
	public void render(GL gl)
	{						
		texture.bind();					
		for(int i = 0; i < pointCount; i++)
		{
			getColor(i).color(gl);
			gl.glTexCoord2f(u[i], v[i]);
			gl.glVertex3f(point[i].x, point[i].y, point[i].z);				
		}				
	}		
}
